﻿using AutoMapper;
using MediatR;
using Microsoft.IdentityModel.Tokens;
using MyMES.Domain.yjb;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Read.API.Application.Commends.settingCommand;
using MyMES.Read.API.FileNlog;

namespace MyMES.Read.API.Application.CommendHandlers.settingCommand
{
    public class WorkOrderInfoCommandHandlers : IRequestHandler<WorkOrderInfoCommand, APIPaging<List<WorkOrderInfo>>>
    {

        private readonly IBaseRepository<WorkOrderInfo> accreop;
        private readonly Nolgs nlogselvse;
        private readonly IMapper mapper;

        public WorkOrderInfoCommandHandlers(IBaseRepository<WorkOrderInfo> accreop, Nolgs nlogselvse)
        {
            this.accreop = accreop;
            this.nlogselvse = nlogselvse;
            this.mapper = mapper;
        }
        /// <summary>
        /// 工单显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIPaging<List<WorkOrderInfo>>> Handle(WorkOrderInfoCommand request, CancellationToken cancellationToken)
        {
            var startTime = System.DateTime.Now;
            try
            {
                APIPaging<List<WorkOrderInfo>> result = new APIPaging<List<WorkOrderInfo>>();
                result.Code = APIEnum.查询失败;
                result.Msg = "查询成功";
                string sql = "select * from WorkOrderInfo where IsDelete=true ORDER BY WorkOrderInfoId DESC";

                var list = await accreop.SelectAsync(sql);

                if (!string.IsNullOrEmpty(request.WorkOrderCode))
                {
                    list = list.Where(x => x.WorkOrderCode.Contains(request.WorkOrderCode)).ToList();
                }
                if (!string.IsNullOrEmpty(request.WorkOrderName))
                {
                    list = list.Where(x => x.WorkOrderName.Contains(request.WorkOrderName)).ToList();
                }
                if (!string.IsNullOrEmpty(request.WorkOrderSource))
                {
                    list = list.Where(x => x.WorkOrderSource.Contains(request.WorkOrderSource)).ToList();
                }



                if (!string.IsNullOrEmpty(request.ProductCode))
                {
                    list = list.Where(x => x.ProductCode.Contains(request.ProductCode)).ToList();
                }



                if (!string.IsNullOrEmpty(request.ProductName))
                {
                    list = list.Where(x => x.ProductName.Contains(request.ProductName)).ToList();
                }
                if (!string.IsNullOrEmpty(request.CustomerCode))
                {
                    list = list.Where(x => x.CustomerCode.Contains(request.CustomerCode)).ToList();
                }
                if (!string.IsNullOrEmpty(request.CustomerName))
                {
                    list = list.Where(x => x.CustomerName.Contains(request.CustomerName)).ToList();
                }
                if (request.DemandDate != null)
                {
                    list = list.Where(x => x.DemandDate == request.DemandDate).ToList();
                }

                result.TotalCount = list.Count;
                result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.Size);
                list = list.Skip((request.Index - 1) * request.Size).Take(request.Size).ToList();

                result.data = list;
                return result;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                var endTime = System.DateTime.Now;

                var durationInDays = (endTime - startTime).Days;
                nlogselvse.GetNlogs(
                    request.CustomerName ?? "Unknow", "工序排产显示列表", "False", durationInDays, "异常信息"
                    );
            }
        }
    }
}
